package com.sphd.education.activitys;

import android.content.ClipData;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.JsonObject;
import com.sphd.education.EducationApplication;
import com.sphd.education.R;
import com.sphd.education.adapter.MyAddNameLessonAdapter;
import com.sphd.education.base.BaseActivity;
import com.sphd.education.info.CityGpsInfo;
import com.sphd.education.info.EducationIntentKey;
import com.sphd.education.info.LessonInfo;
import com.sphd.education.info.MyInfo;
import com.sphd.education.modle.EducationUrl;
import com.sphd.education.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
/*
*我的报名界面
* */
public class MyAddNameLessonActivity extends BaseActivity implements PopupMenu.OnMenuItemClickListener {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.my_addname_lesson)
    ListView myAddnameLesson;
    private MyInfo myInfo;
    String url;
    MyAddNameLessonAdapter lessonAdapter;
    List<LessonInfo> infos;
    @Bind(R.id.my_refresh)
    SwipeRefreshLayout myRefresh;
    private Bundle bundle;
    private CityGpsInfo cityGpsInfo;
    private String cityName;
    MenuItem view;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_add_name_lesson);
        ButterKnife.bind(this);
        setSupportActionBar(toolbar);
        initView();
    }

    @Override
    public void initView() {
        bundle = new Bundle();
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();

            }
        });
        myRefresh.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
        myInfo= DataSupport.findLast(MyInfo.class);
        cityGpsInfo = DataSupport.findLast(CityGpsInfo.class);
        if (cityGpsInfo.isQiecity()) {
            cityName = cityGpsInfo.getOthercity();
        } else {
            cityName = cityGpsInfo.getLoacalcity();
        }
        infos=new ArrayList<>();
        lessonAdapter=new MyAddNameLessonAdapter(this,infos);
        url = EducationUrl.HttpTitle+EducationUrl.MyCourse+"memberId="+myInfo.getUserid();
        myNet();
        myRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                infos.clear();
                myNet();
            }
        });
        myAddnameLesson.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent =new Intent();
                bundle.putInt("lessonId", infos.get(position).getLesson_id());
                bundle.putString(EducationIntentKey.FLAG, EducationIntentKey.LESSON_INFO);
                intent.putExtra(EducationIntentKey.LESSON, bundle);
                intent.setClass(MyAddNameLessonActivity.this, LessonInfoActivity.class);
                startActivity(intent);
            }
        });
    }
    @Override
    public void findViewById() {

    }

    @Override
    public void myNet() {
        Log.e("URL", url);
        StringRequest jsonObjectRequest =new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.e("response", response);
                getList(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(6 * 1000, 2, 1.0f));
        EducationApplication.getRequestQueue().add(jsonObjectRequest);
    }

    private void getList(String response) {
        try {
            JSONArray jsonArray =new JSONArray(response);
            for (int i=0;i<jsonArray.length();i++){
                JSONObject jsonObject =jsonArray.getJSONObject(i);
                LessonInfo lessonInfo=new LessonInfo();
                String a =jsonObject.getString("startDate");
                String c=a.replaceAll("-", ".");
                Log.e("c", c + "");
                String [] strs = c.split("[ ]");
                lessonInfo.setBeginTime(strs[0].toString());
                JSONObject object =jsonObject.getJSONObject("organization");
                lessonInfo.setSchoolName(object.getString("name"));
                lessonInfo.setPhone(object.getString("phone"));
                lessonInfo.setImage(jsonObject.getString("image"));
                lessonInfo.setName(jsonObject.getString("name"));
                lessonInfo.setDescription(jsonObject.getString("description"));
                lessonInfo.setRealTuition(jsonObject.getString("realTuition"));
                lessonInfo.setTuitionStatus(jsonObject.getInt("tuitionStatus"));
                if (jsonObject.has("id"))
                    lessonInfo.setLesson_id(jsonObject.getInt("id"));
                infos.add(lessonInfo);
            }
            myAddnameLesson.setAdapter(lessonAdapter);
            myRefresh.setRefreshing(false);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_my_add_name_lesson, menu);
        view = (MenuItem) menu.findItem(R.id.choose);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId()==R.id.submenu1){
            url = EducationUrl.HttpTitle+EducationUrl.MyCourse+"memberId="+myInfo.getUserid()+"&type=2";
            infos.clear();
            view.setTitle("不缴费");
            myNet();
        }else if (item.getItemId()==R.id.submenu2){
            url = EducationUrl.HttpTitle+EducationUrl.MyCourse+"memberId="+myInfo.getUserid()+"&type=0";
            infos.clear();
            view.setTitle("已报名");
            myNet();
        }else if (item.getItemId()==R.id.submenu3){
            url = EducationUrl.HttpTitle+EducationUrl.MyCourse+"memberId="+myInfo.getUserid()+"&type=1";
            infos.clear();
            view.setTitle("报名成功");
            myNet();
        }else if (item.getItemId()==R.id.submenu4){
            url = EducationUrl.HttpTitle+EducationUrl.MyCourse+"memberId="+myInfo.getUserid();
            infos.clear();
            view.setTitle("全部");
            myNet();
        }

        return super.onOptionsItemSelected(item);
    }
    @Override
    public boolean onMenuItemClick(MenuItem item) {

        return false;
    }
}
